﻿using System.Linq;
using AdventureWorks.Domain;
using NHibernate;
using AdventureWorks.DataAccess.SearchCriterias;
using NHibernate.Linq;
using AdventureWorks.DataAccess.Utils;

namespace AdventureWorks.DataAccess.repository
{
    public class ProductInventoryRepository : IProductInventoryRepository
    {
        public IQueryable<ProductInventory> GetProductInventory(ISession session, ProductInventorySearchCriteria searchCriteria)
        {
            return session.Query<ProductInventory>()
               .NullableWhere(x => x.Product.Name == searchCriteria.Name)
               .NullableWhere(x => x.Product.ProductNumber == searchCriteria.ProductNumber)
               .NullableWhere(x => x.Product.Color == searchCriteria.Color)
               .NullableWhere(x => x.Product.StandardCost == searchCriteria.StandardCost)
               .NullableWhere(x => x.Product.SubCategory.Name == searchCriteria.SubCategory)
               .NullableWhere(x => (int)x.Product.SubCategory.Category == searchCriteria.Category)
               .NullableWhere(x => x.Location.Name == searchCriteria.LocationName)
               .NullableWhere(x => x.Quantity == searchCriteria.Quantity);
        }
    }
}
